<?php

namespace App\Admin\Controllers;

use App\Admin\Extensions\ExcelExport\CarExpoter;
use App\Admin\Extensions\Tools\ImportButton;
use App\Models\Area;
use App\Models\Car;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;

class CarController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index(Content $content)
    {
            return $content->header('旅游车辆')
                        ->body($this->grid());
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id,Content $content)
    {
        return $content->header('编辑')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create(Content $content)
    {
        return $content->header('新建')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid=new Grid(new Car);
        $grid->tools(function ($tools) {
            $tools->append(new ImportButton('cars'));
        });
        $grid->actions(function ($actions) {
            $actions->disableView();
        });
        $grid->filter(function($filter){
            // 去掉默认的id过滤器
            $filter->disableIdFilter();
            // 在这里添加字段过滤器
            $filter->like('license_plate_number', '车牌号');
            $filter->equal('area_id', '区县')->select(Area::pluck('title','id'));

        });
        $grid->exporter(new CarExpoter());
        $grid->id('ID')->sortable();
        $grid->license_plate_number('车牌号');
        $grid->area_id('车籍地')->using(Area::all()->pluck('title','id')->toArray());
        //$grid->type('客车类型');
        $grid->company('所属公司');
        $grid->driver('司机');
        return $grid;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Car);
        $form->select('area_id','车籍地')->options(Area::all()->pluck('title','id'));
        $form->checkbox('scope','经营范围')->options(config('options.car_scope'));
        $form->text('license_plate_number','车牌号');
        //$form->text('type','客车类型');
        $form->text('company','所属公司');
        return $form;
    }
}
